﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace TravelLink.Common {
    public sealed class MailSender {

        /// <summary>
        /// 发送注册验证邮件
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="userEmail"></param>
        public static void SendMailByRegisterValidation(Guid userID, string userEmail) {
            var toList = new List<string>(1);
            toList.Add(userEmail);
            string subject = "Travel Link Recruitment | 旅业链接招聘 - 注册验证";
            string body = @"  <style type='text/css'>
        .cn
        {
            font: 500 12px/18px '微软雅黑' , 'Times New Roma' ,Arial, Helvetica, sans-serif;
        }
        .en
        {
            font: 500 14px/18px 'Times New Roma' , '微软雅黑' ,Arial, Helvetica, sans-serif;
        }
    </style>
    <div class='cn'>
        <p>
            感谢您注册旅业链接-招聘网站！</p>
        <p>
            请点击<a href='#LINK#'>这里</a>确认您的注册邮件地址。</p>
        <p>
            如以上链接无法打开，可以复制以下地址至您的浏览器地址栏进行验证。</p>
        <p>
            #LINK#
        </p>
        <p>
            您需要登录您的账户来验证您的注册邮件地址，请确保您登录所使用的是现有的邮件地址。</p>
    </div>
    <div class='en'>
        <p>
            Thank you for your registration on Travel Link Recruitment!</p>
        <p>
            Click <a href='#LINK#'>here</a> to confirm your email address.
        </p>
        <p>
            If the above link does not work, you can paste the following address into your browser:</p>
        <p>
            #LINK#</p>
        <p>
            You will be asked to log into your account to confirm this email address. Be sure
            to log in with your current primary email address.
        </p>
        <p>
            Thank you for using Travel Link Recruitment!</p>
        <p>
           <a href='http://www.travellinkrecruitment.com/'>http://www.travellinkrecruitment.com</a></p>
    </div>".Replace("#LINK#", Utility.SiteRoot + "/Member/ActiveEmail.aspx?dt=" + DateTime.Now.Ticks + "&st=" + Guid.NewGuid() + "&m=" + userID.ToString() + "&ml=" + userEmail.ToString());
            bool isBodyHtml = true;
            SendMail(toList, null, isBodyHtml, subject, body);
        }

        /// <summary>
        /// 发送忘记密码邮件
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="userEmail"></param>
        public static string SendMailByForgetPassword(Guid userID, string userEmail) {
            var toList = new List<string>(1);
            toList.Add(userEmail);
            string subject = "Travel Link Recruitment | 旅业链接招聘 - 取回密码";
            string body = @"<style type='text/css'>
        .cn
        {
            font: 500 16px/30px '微软雅黑' , 'Times New Roma' ,Arial, Helvetica, sans-serif;
        }
        .en
        {
            font: 500 18px/30px 'Times New Roma' , '微软雅黑' ,Arial, Helvetica, sans-serif;
        }
    </style>
    <div class='cn'>
        <p>
            感谢您登录旅业链接-招聘网站！</p>
        <p>
            如您忘记密码，请以此密码XXXXX登陆我们的网站，登陆后可前往 #LINK# 重置您的密码。</p>
    </div>
    <div class='en'>
        <p>
            Thank you for sign on Travel Link Recruitment!</p>
        <p>
            Please sign on by password XXXXX, and you are able to reset your new password through
            #LINK#.
        </p>
        <p>
            <a href='http://www.travellinkrecruitment.com/'>http://www.travellinkrecruitment.com</a>
        </p>
    </div>".Replace("#LINK#", Utility.SiteRoot);
            bool isBodyHtml = true;

            int pwdLength = 6;
            string randomPwd = Utility.RandomPassword(PwdType.NUMCHAR, pwdLength);
            if (!string.IsNullOrEmpty(randomPwd) && randomPwd.Length == pwdLength) {
                body = body.Replace("XXXXX", randomPwd);
                SendMail(toList, null, isBodyHtml, subject, body);
                return randomPwd;
            }
            else {
                return string.Empty;
                // throw new Exception("生成随机密码失败！");
            }

        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="toList">收件人</param>
        /// <param name="ccList">抄送</param>
        /// <param name="isBodyHtml">是否HTML邮件</param>
        /// <param name="subject">主题</param>
        /// <param name="body">邮件内容</param>
        private static void SendMail(List<string> toList, List<string> ccList, bool isBodyHtml, string subject, string body) {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            if (toList == null || toList.Count == 0) {
                throw new Exception("未指定收件人！");
            }
            foreach (var to in toList) {
                msg.To.Add(to);
            }
            if (ccList != null && ccList.Count > 0) {
                foreach (var cc in ccList) {
                    msg.CC.Add(cc);
                }
            }
            //msg.From = new MailAddress(MailSeanderConfig.Address, MailSeanderConfig.DisplayName, System.Text.Encoding.UTF8);
            msg.From = new MailAddress(MailSeanderConfig.Address);
            msg.Subject = subject;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = body;
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = isBodyHtml;
            msg.Priority = MailPriority.High;

            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential(MailSeanderConfig.UserName, MailSeanderConfig.Password);
            client.Host = MailSeanderConfig.Smtp_Server;
            client.Send(msg);
        }

        public static void SendMailByRecommend(string content, List<string> toList) {
            string subject = "Travel Link Recruitment | 旅业链接招聘  - 职位推荐";
            string body = content;
            bool isBodyHtml = true;
            SendMail(toList, null, isBodyHtml, subject, body);
        }


        public static void SendMailByRecommend(string content, List<string> positionList, string toMail) {
            string subject = "Travel Link Recruitment | 旅业链接招聘  - 职位推荐";
            string body = @"<style type='text/css'>
        .cn
        {
            font: 500 16px/30px '微软雅黑' , 'Times New Roma' ,Arial, Helvetica, sans-serif;
        }
        .en
        {
            font: 500 18px/30px 'Times New Roma' , '微软雅黑' ,Arial, Helvetica, sans-serif; 
        }
    </style>
    <div class='cn'>
        XXXXX
        <p> 职位推荐 #LINK# </p>
    </div>".Replace("#LINK#", Utility.SiteRoot);
            bool isBodyHtml = true;
            string positionStr = string.Empty;
            foreach (var postionInfo in positionList) {
                positionStr += string.Format("<p>{0}</p>", postionInfo);
            }
            body = body.Replace("XXXXX", positionStr);
            var toList = new List<string>(1);
            toList.Add(toMail);
            SendMail(toList, null, isBodyHtml, subject, body);
        }

        public static void SendMailByRecommendByAuto(string userName, List<string> positionList, string toMail) {

            string subject = "Travel Link Recruitment | 旅业链接招聘  - 职位推荐";
            string body = @"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
    <title>职位信息推送页面</title>
    <style type='text/css'>
<!--
body { margin: 0 auto; font-size:12px;}
li,dl{list-style-type:none; margin:0; padding:0;vertical-align:top;}
a:link,a:visited{color:#000000;text-decoration: none;}
a:hover {color: #cd0000;text-decoration: none;}
a:active {text-decoration: none;}

#job_box{ width:611px; margin: 0 auto; padding-top:15px;}
#job_left1{ width:5px; float:left; background:url(http://www.travellinkrecruitment.com/images/job_pic_03.jpg); height:781px;}
#job_conten1{ width:599px;_width:597px ; height:781px; float:left; border:1px solid #d1d1d1;}
#job_right1{ width:5px; float:right;background:url(http://www.travellinkrecruitment.com/images/job_pic_05.jpg); height:781px;}
#job_c_top{ width:599px; _width:597px; margin-top:33px; text-align:center;}
#job_c_block{ width:568px; height:10px; margin:0 auto; padding-top:11px;}
#job_c_words{ width:556px; font-size:12px; color:#757474; padding:25px 0 0 45px; line-height:20px;}
#job_c_refer{ width:556px; padding-left:45px; color:#5a479a; padding-top:15px;}
#job_c_picc{ width:568px; height:37px !important; background:url(http://www.travellinkrecruitment.com/images/job_pic_17.jpg);  margin:13px 0 0 15px;}
#job_news_zp{ width:568px; height:52px;margin:25px 0 0 15px;}
.job_name{ font-size:14px;  display:block; height:8px;}
.job_picc_change{ text-align:right; display:block; line-height:40px; color:#d1d1d1; padding-right:17px;}
#job_news_zp_titel{ width:500px; display:block; font-size:14px; color:#493292; font-weight:bold; padding-left:10px;}
#job_news_zp_recr{ width:480px; height:24px; padding:12px 0 0 10px; color:#020000; float:left;}
#job_news_zp_view{ float:right; width:69px; height:12px; margin-top:14px;}
#job_news_zp_ine{ width:568px; height:1px;}
#job_bottom{ width:479px; margin:40px 0 0 50px;}
.job_bm_left{ float:left; margin-top:5px;}
.job_bm_right{ float:right; padding-left:15px;}
-->
</style>
</head>
<body>
    <div id='job_box'>
        <div id='job_left1'>
        </div>
        <div id='job_conten1'>
            <div id='job_c_top'>
                <img src='http://www.travellinkrecruitment.com/images/job_pic_08.jpg' />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <img src='http://www.travellinkrecruitment.com/images/job_pic_11.jpg' width='207'
                    height='25' /></div>
            <div id='job_c_block'>
                <img src='http://www.travellinkrecruitment.com/images/job_pic_15.jpg' width='568'
                    height='10' /></div>
            <div id='job_c_words'>
                <span class='job_name'>#USERNAME#， 您好！</span>
                <br />
                旅业链接&nbsp;|&nbsp;招聘旨在为旅游行业的个人和企业寻求发展提供更广阔的天地。
                <br />
                为旅游业内人士提供更好的职业发展机会，为业内企业招募优秀人才，是我们的不懈追求！</div>
            <div id='job_c_refer'>
                <a href='' style='color: #5a479a;'>敬请查阅您在旅业链接</a>&nbsp;|&nbsp;<a href='' style='color: #5a479a;'>招聘网站订阅职位信息如下</a></div>
            <div id='job_c_picc'>
                <span class='job_picc_change'><a href='http://www.travellinkrecruitment.com' style='color: #d1d1d1;'>更改</a>/ <a href=''
                    style='color: #d1d1d1;'>&nbsp;&nbsp;&nbsp;&nbsp;</a></span></div>
#CONTENT#

 <div id='job_bottom'>
                <div class='job_bm_left'>
                    <a href='http://www.travellinkrecruitment.com'>
                        <img src='http://www.travellinkrecruitment.com/images/job_pic_29.jpg' border='0' /></a></div>
                <div class='job_bm_right'>
                    <a href='http://www.travellinkrecruitment.com'>
                        <img src='http://www.travellinkrecruitment.com/images/job_pic_26.jpg' width='118'
                            height='38' border='0' /></a></div>
            </div>
        </div>
        <div id='job_right1'>
        </div>
    </div>
</body>
</html>
";
            bool isBodyHtml = true;
            string positionStr = string.Empty;

            foreach (var postionInfo in positionList) {
                positionStr += postionInfo;
            }
            body = body.Replace("#USERNAME#", userName);
            body = body.Replace("#CONTENT#", positionStr);
            var toList = new List<string>(1);
            toList.Add(toMail);
            SendMail(toList, null, isBodyHtml, subject, body);
        }
    }
}
